const http = require('../../../../utils/util')
const Base64 = require('../../../../utils/base')
const time_data = require("../../../../utils/data.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
		order_id:'', //订单id
		order_detail:{}, //订单详情
		type:null,
		store_id:'',
		backPage:null,
		isbackStore:false,
		loading:false,
    },

	//获取订单详情数据
	getOrderDetail(){
		let _this=this
		let data={
			user_id: wx.getStorageSync('user_id'),
			user_token: wx.getStorageSync('user_token'),
			order_id:this.data.order_id,
			type:this.data.type,
		}
		http.post('order/index',Base64.encode(JSON.stringify(data))).then((res)=>{
			if(res.status==1){
				this.setData({
					order_detail:res.result
				})
			}else{
				wx.showToast({
					title:'获取订单详情数据失败',
					icon:'none'
				})
			}
		})
	},
	//申请退款
	refund(){
		let _this=this
		wx.showModal({
		  title: '提示',
		  content: '确定要进行退款操作吗？',
		  success (res) {
		    if (res.confirm) {
		      // 用户进行退款
			  _this.rebate()
		    }
		  }
		})
	},
	//退款接口
	rebate(){
		let _this=this
		let data={
			user_id:wx.getStorageSync('user_id'),
			user_token:wx.getStorageSync('user_token'),
			type:this.data.type==1?2:1,
			order_id:this.data.order_id
		}
		http.post('user/orderRefund',Base64.encode(JSON.stringify(data))).then(res=>{
			if(res.status==1){
				wx.showToast({title:'退款成功',icon:'none'})
				setTimeout(()=>{
					_this.getOrderDetail()
				},1000)
			}else{
				wx.showToast({
					title:res.message,
					icon:'none'
				})
			}
		})
	},
	//返回逻辑
	goBack(){
		let _this=this
		if(getApp().globalData.is_from){
			wx.reLaunch({
				url:'/pages/index/store/store?id='+_this.data.store_id,
				success:()=>{
					getApp().globalData.is_from=false
				}
			})
		}else{
			wx.navigateBack({
				delta:1
			})
		}
	},
	//座位开门
	openDoor(){
		
		let that=this
		
		//判断是否到开门时间
		let isOpen = this.compareTime()
		if(!isOpen){
			return wx.showToast({title:'预约开始时间前15分钟可点击开门',icon:'none'})
		}
		
		//判断是否距离门店不超过50米
		
		wx.getLocation({
		 type: 'gcj02',
		 isHighAccuracy:true,
		 success (res) {
			 
			 that.setData({
			 	loading:true
			 },()=>{
			 	setTimeout(()=>{
			 	that.setData({
			 		loading:false
			 	}) 
			 	},3000)
			 })
			  let data={
			  	user_id: wx.getStorageSync('user_id'),
			  	user_token: wx.getStorageSync('user_token'),
			  	store_id:that.data.order_detail.store_id,
			 	latitude:res.latitude,
			 	longitude:res.longitude
			  }
			  http.post('/store/openDoor',Base64.encode(JSON.stringify(data))).then((res)=>{
			 	if(res.status==1){
			 		wx.showToast({
			 			title:'开门成功',
			 			icon:'none'
			 		})
					setTimeout(()=>{
						that.getOrderDetail()
					},1000)
			 	}else{
			 		wx.showToast({
			 			title:res.message,
			 			icon:'none'
			 		})
			 	}
			  }) 
			 
			 
		 },
		 fail () {
			return wx.showToast({
				 title:'获取定位失败',
				 icon:'none'
			})
		 }
		})
		
		
	},
	
	//会议室开门
	
	
	
	
	//获取当前时间戳和预约开始时间作比较
	compareTime(){
		
		let date=this.data.order_detail.time+' '+this.data.order_detail.sub_start
		let timer= date.replace(/-/g,'/')
		
		//当前时间戳
		let nowtime=new Date().getTime()
		//预约开始时间戳
		let starttime=new Date(timer).getTime()
		let hours=this.data.order_detail.time_slot*60*60*1000
		//
		let s=starttime-900000
		let end=starttime+hours
		if(s<=nowtime && nowtime<end){
			return true
		}else{
			return false
		}
	},


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		//拿到订单id
		if(options.params){
			let params=JSON.parse(options.params)
			this.setData({
				order_id:params.order_id,
				type:params.type,
				store_id:params.store_id?params.store_id:''
			})
			this.getOrderDetail()
		}
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
		
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
		
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})